package com.wsoft;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.wsoft.entity.BaseEntity;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Collections;

/**
 * 代码生成器
 *
 * @author: juiet
 * @date: 2024年08月24日 20:06
 */
public class CodeGenerator {
    public static String url = "jdbc:mysql://121.227.153.93:3306/wsoft?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull";
    public static String username = "wsoft";
    public static String password = "wsoft";

    /**
     *作者
     */
    public static String auth = "Z001";
    /**
     * 模块 wsoft-ass-module/wsoft-ass-module-system/
     */
    public static String moduleName = "wsoft-ass-hr/wsoft-ass-hr-pm/";
    public static String tableName = "hr_pm_warning";
    public static String packages = "com.wsoft.hr.pm";

    public static void main(String[] args) {

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder
                        .author(auth).enableSpringdoc()
                        .disableOpenDir()
                        .outputDir(moduleName+"src/main/java")
                        .commentDate("yyyy年MM月dd日 HH:mm:ss")
                )
                .packageConfig(builder -> builder
                        .parent(packages)
                        .controller("controller")
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder.addInclude(tableName)
                        .entityBuilder()
                        .superClass(BaseEntity.class)
                        .disableSerialVersionUID()
                        .enableFileOverride()
                        .addIgnoreColumns("id", "create_by", "create_time", "update_by", "update_time","deleted")
                        .formatFileName("%sEntity")
                        .enableLombok()
                        .enableTableFieldAnnotation()


                        .controllerBuilder()
                        .enableRestStyle()

                        .serviceBuilder()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

    public static void main1(String[] args) {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("juiet") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.wsoft.mbg") // 设置父包名
//                                .moduleName("system") // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.xml, Paths.get(System.getProperty("user.dir")) + "/src/main/resources")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude("ts_user") // 设置需要生成的表名
//                                .addTablePrefix("t_", "c_") // 设置过滤表前缀
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
